<?php
class Gallery_WidgetController extends Clu_Controller_Action_Widget
{
	
	public function init()
	{
		parent::init();
	}
	
	public function displayThumbsAction()
	{
		$id = $this->getParam("id");
		if ($this->issetParam("mode"))
			$this->viewParams['mode'] = $this->getParam("mode");
		//require_once(APP_PATH . "/modules/node/Model/NodeService.php");
		$nService = new node_Model_NodeService();
		$nodeTypeInfo = $nService->getNodeTypeInfo("gallery");
		$ret = $nService->getNodeData($nodeTypeInfo, $id);
		if ($ret['success'])
		{
			$this->viewParams['gallery'] = $ret['node'];
			$this->viewParams['id'] = $id;
		}
		else
			$this->viewParams['gallery'] = null;			
	}
	
	/*
	 * using 4--widget--upload-photos.phtml 
	 * which is a copy of 4--master--upload-photo-uploadify
	 */
	public function uploadPhotosAction()
	{
		$galleryId = $this->getParam("id");
		if ($this->issetParam("content_id"))
			$this->viewParams['content_id'] = $this->getParam("content_id");
		else
			$this->viewParams['content_id'] = 1; //not important
		//$galleryId =123223;
		//require_once(APP_PATH . "/modules/node/Model/NodeService.php");
		$nService = new node_Model_NodeService();
		$nodeTypeInfo = $nService->getNodeTypeInfo("gallery");
		$ret = $nService->getNodeData($nodeTypeInfo, $galleryId);
		
		//die_a($ret);
		if ($ret['success'])
			$this->viewParams["gallery"] = $ret['node'];
		else
		{
			$this->viewParams["gallery"] = null;
		}
	} 
	
	public function addGalleryAction()
	{
		//get the content_id & content module name (place or event or node);
		if ($this->issetParam('content_id'))
			$this->viewParams['content_id'] = $this->getParam('content_id');
		$this->viewParams['content_type'] = $this->getParam('content_type');
		if ($this->viewParams['content_type'] == "node")
			$this->viewParams['node_type'] = $this->getParam('node_type');
	}
	
	/* side gallery view for different content */ 
    public function sideGalleryAction()
    {
    	$contentType = $this->getParam('content_type');
    	if ($contentType == "gallery")
    		return;
    	
    	if ($this->issetParam('content_id'))
    	{
    		$contentId = $this->getParam('content_id');
    		$this->viewParams['content_id'] = $contentId;
    	}
		$this->viewParams['content_type'] = $contentType;
		
    	if ($this->issetParam('gallery_id'))
    		$galleryId = $this->getParam('gallery_id');
		if (!isset($galleryId))
		{
			$this->viewParams['id'] = '';
			return;		
		}
		//require_once(APP_PATH . "/modules/node/Model/NodeService.php");
		$nService = new node_Model_NodeService();
		$nodeTypeInfo = $nService->getNodeTypeInfo("gallery");
		$ret = $nService->getNodeData($nodeTypeInfo, $galleryId);
		
		if ($ret['success'])
		{
			$gallery = $ret['node'];
			$this->viewParams['images'] = unserialize($gallery['images']);
			$this->viewParams['id'] = $galleryId;
		}
		else
    	{
			$this->viewParams['id'] = '';
			return;		
		}				     	
    }
    
    /*
     * Get a few albums here
     */
    public function featuredAlbumsHomeAction()
    {
		//require_once(APP_PATH . "/modules/node/Model/NodeService.php");
		$nService = new node_Model_NodeService();
		$nodeTypeInfo = $nService->getNodeTypeInfo("gallery");
		//get the items from `weights` table first
				//require_once (APP_PATH . "/modules/admin/Model/Weight.php");
    	$mWeight = new admin_Model_Weight();

    	//now get all the content_id from this $widgetPositionId;
    	$data['widget_position_id'] = $this->widget['id'] ;
    	$currentItems = $mWeight->searchCurrentItemsByWidgetPositionId($data);
    	$contents = array();
		foreach ($currentItems as $item)
		{
			$tmp = $nService->getNodeData($nodeTypeInfo, $item['content_id']);
			$contents[] = $tmp['node'];
		}
		$this->viewParams['itemList'] = $contents;
    }
    
    /**
     * Other galleries displayed in region 5
	 */
    public function relatedGalleriesAction()
    {
    	$galleryId = $this->getParam("id");
    	//die($galleryId);
    	//contentType being nodeType
		//require_once(APP_PATH . "/modules/node/Model/NodeService.php");
		$model = new node_Model_NodeService();
		$nodeTypeInfo = $model->getNodeTypeInfo("gallery");
		//die_a($nodeTypeInfo);
		$node = $model->findNodeByPrimary($nodeTypeInfo, $galleryId);
		$type = $node->type;
		unset($node);
		$data = array ("type" => $type, "page" => 1 + $galleryId % 10 , "items_perpage" => 12);
		$select = $model->search($data, $nodeTypeInfo);
		$paginator = $this->generatePaginator($select, $data['page'], $data['items_perpage'], 7, 'Jumping');
        //$searchUrl = encode_search_url($data, $contentType);
		$this->viewParams['itemList'] = $paginator->getCurrentItems();
		//die_a($this->viewParams['items']);
		$this->viewParams['content_type'] = "gallery";			
    }
    
    /*
     * Region 5 galleries used when searching something, just something nice..
     */
    public function featuredContentGalleriesAction()
    {
    	$contentType = $this->getParam("content_type");
		//require_once(APP_PATH . "/modules/node/Model/NodeService.php");
		$model = new node_Model_NodeService();
		$nodeTypeInfo = $model->getNodeTypeInfo("gallery");

		if ($contentType == "event")
			$category = -2;
		elseif ($contentType == "place")
			$category = -1;
			
		$data = array ("category" => $category, "page" => 1 , "items_perpage" => 12);
		$select = $model->search($data, $nodeTypeInfo);
		$paginator = $this->generatePaginator($select, $data['page'], $data['items_perpage'], 7, 'Jumping');
        //$searchUrl = encode_search_url($data, $contentType);
		$this->viewParams['itemList'] = $paginator->getCurrentItems();
		$this->viewParams['content_type'] = $contentType;
    }
    
}
?>